package com.groupbuying.store.mq;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.groupbuying.store.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @anthor wenguoqiang
 * @date: 2019/06/10 8:57
 * @company 够样
 * @des
 **/
@Component
public class StoreResultListener implements MessageListener {

    public static final Logger LOGGER = LoggerFactory.getLogger(StoreResultListener.class);

    @Autowired
    private StoreReportResult storeReportResult;

    @Autowired
    private StoreVirtualDealResult storeVirtualDealResult;

    @Autowired
    private StoreWithdrawDealResult storeWithdrawDealResult;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {

        //Topic校验Tag校验
        if (!message.getTopic().equals(StoreMQConfig.TOPIC) || StringUtils.isEmpty(message.getTag())) {
            LOGGER.error("Topic不符合订阅要求，Topic：" + message.getTopic() + "或者Tag为空，Tag：" + message.getTag());
            return Action.CommitMessage;
        }
        //获取消息重试次数
        int reconsume = message.getReconsumeTimes();
        if (reconsume == 3) {
            LOGGER.error("该消息重复消费三次，直接标识成功");
            return Action.CommitMessage;

        }
        //解析消息体
        Map<String, Object> result = JSONUtils.jsonToMap(new String(message.getBody()));

        /**
         * 通过TAG来判断处理方式
         *
         */
        LOGGER.info("获取消息：{}" , result.toString());
        boolean handleResult = false;
        //微信支付回执处理
        if (StoreMQConfig.YSREPORT.equals(message.getTag())) {
            LOGGER.error("===========商户报备异步回执处理：============");
            handleResult = storeReportResult.dealAsyResult(result);
            //积分支付回执处理
        }else if (StoreMQConfig.YSREQUERTREPORTRESULT.equals(message.getTag())){
            LOGGER.error("===========商户报备同步结果回执处理：============");
            handleResult = storeReportResult.dealSynResult(result);
        }else if(StoreMQConfig.YSREQUERTREPORTRESULTQUERY.equals(message.getTag())){
            LOGGER.error("===========商户报备查询回执处理：============");
            handleResult = storeReportResult.dealQueryResult(result);
        }else if (StoreMQConfig.YSREQUERTERRORRESULT.equals(message.getTag())){
            LOGGER.error("===========商户报备异常回执回执处理：============");
            handleResult = storeReportResult.dealQueryResult(result);
        }else if(StoreMQConfig.NOTIFYSTORE_TAG.equals(message.getTag())){
            LOGGER.error("===========商户分账虚拟账户处理：============");
            handleResult = storeVirtualDealResult.dealSettleResult(result);
        }else if(StoreMQConfig.WITHDRAW.equals(message.getTag())){
            LOGGER.error("===========商户提现虚拟账户处理：============");
            handleResult = storeWithdrawDealResult.dealWithdraw();
        }
        if (!handleResult) {
            LOGGER.error("处理失败；Tag：" + message.getTag());
        }
        return  Action.CommitMessage;
    }
}
